<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>登陆</title>
	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
	<style>
		body{
			background: #f3f3f3;
		}
		.container{
			margin: 64px auto auto auto;
			width: 400px;
		}
		.title{
			text-align: center;
		}
		.login{
			margin-top: 64px;
		}
		.btn_group{
			text-align: center;
		}
	</style>
</head>
<body>

<div class="container">
	<h1 class="title">登陆</h1>
	<div class="login">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="form-group" id="input1">
					<label class="control-label" for="inputError1" id="inputError1"></label>
					<input type="text" class="form-control" id="emailInput" placeholder="邮箱">
				</div>
				<div class="form-group" id="input2">
					<input type="password" class="form-control" id="pswInput" placeholder="密码">
					<label class="control-label" for="inputError2" id="inputError2"></label>
				</div>
				<div class="btn_group">
					<button type="button" class="btn btn-primary" id="loginBtn">登陆</button>
					<button type="button" class="btn btn-default" id="regBtn">注册</button>
				</div>
				
			</div>
		</div>
	</div>
	
</div>

<script>

function setError1(err){
	if (err.length == 0) {
		$("#input1").removeClass("has-error");
	}else{
		$("#input1").addClass("has-error");
	}
	$("#inputError1").text(err);
}

function setError2(err){
	if (err.length == 0) {
		$("#input2").removeClass("has-error");
	}else{
		$("#input2").addClass("has-error");
	}
	$("#inputError2").text(err);
}

function setDoPost(state){
	if (state) {
		$("#emailInput").attr("disabled", "disabled");
		$("#pswInput").attr("disabled", "disabled");
		$("#loginBtn").attr("disabled", "disabled");
	}else{
		$("#emailInput").removeAttr("disabled");
		$("#pswInput").removeAttr("disabled");
		$("#loginBtn").removeAttr("disabled");
	}
	
}

function doLogin(){
	var email = $("#emailInput").val();
	var password = $("#pswInput").val();
	// var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	// var regex = new RegExp(reg);
	// if (!regex.test(email)) {
	// 	setError1('邮箱不正确');
	// 	return;
	// }else{
	// 	setError1('');
	// }

	if (password.length == 0) {
		setError2('请输入密码');
		return;
	}else{
		setError2('');
	}

	setDoPost(true);
	$.post('login.do',{
		email: email,
		password: password
	}, function(data){
		// console.log(data);
		try{
			var ret = JSON.parse(data);
			switch(ret.error){
				case 0:
					window.location.href = "index.html";
					break;
				case 101:
					setDoPost(false);
					setError1('用户名不存在');
					break;
				case 102:
					setDoPost(false);
					setError2('密码不正确');
					break;
				default:
					alert("系统异常，请稍后重试");
					setDoPost(false);
					break;
			}
		}catch(ex){
			alert("系统异常，请稍后重试");
			setDoPost(false);
		}
		
	})
}

$("#emailInput").keydown(function(e){  
	if (e.keyCode == 13) {
		doLogin();
	}
});
$("#pswInput").keydown(function(e){  
	if (e.keyCode == 13) {
		doLogin();
	}
});

$("#loginBtn").click(function(){
	doLogin();
});

$("#regBtn").click(function(){
	window.location.href = "register.html"
});

</script>
</body>
</html>